<template>
        <tm-overlay v-model:show="props.quanziPoverlay" contentAnimation style="border-radius: 20rpx;"
        @close="close"
        >
            <view @top.stop="">
                <tm-sheet :width="540" style="border-radius: 20rpx;padding: 0;" parenClass="sheet">
                    <view style="height: 470rpx;">
                    <view class="top">
                        入群申请
                    </view>
                    <view class="mes">
                        <text class="mes-name">{{ props.data.nick }}</text>申请加入群
                    </view>
					<view class="fufei" v-if="note?.inviterName!=null && note?.inviterName!=''">邀请人：{{note?.inviterName}}&nbsp;({{note?.inviterId}}）</view>
                    <view class="fufei">{{ props.data.groupName}}</view>
                    <!-- <view class="laiyuan">
                        <text class="from">来自</text>发现-个人主页
                    </view> -->
                    <view class="info">
                        <text>
                            {{note.msg||props.data.nick}}
                        </text>
                    </view>
                    </view>
                    <view style="height: 220rpx;margin-top:80rpx;">
                    <view class="btn">
                        <view class="tongyi" @tap="agree">同意</view>
                        <view class="jujue" @tap="refuse">拒绝</view>
                    </view>
                </view>
                    <!-- <view class=" types">
                        <view class="tongyi">
                            已同意
                        </view>
                        <view class="jujue">已拒绝</view>
                    </view> -->
                </tm-sheet>
            </view>
        </tm-overlay>
</template>
<script lang="ts" setup>
import { ref, computed, defineProps, defineEmits } from 'vue'
import { onShow, onLoad } from '@dcloudio/uni-app'
import { vistorToMember,sendGroupMessage } from '@/api/api'
import { handleGroupApplication,getGroupMemberProfile,createCustomMessage } from '@/hooks/imHooks'
import { generateSevenDigitRandomNumber } from '@/utils/utils'
const showWin = ref(true)
const pos = ref('bottom');
const props: any = defineProps({
    quanziPoverlay: Boolean,
    data:Object
});
const note = computed(() => {
	if(props.data.application.note){
		return JSON.parse(props.data.application.note);
	}else{
		return null
	}
})
const emit = defineEmits(['closequanziProps','showMsg','updateAddGroup']);
// 同意好友申请
let loading = false
const agree = async () => {
	if(loading)return
	loading = true
    const { applicant,groupID } = props.data;
	
	
    const parmas = {
        groupID,
        userIDList:[applicant],
        memberCustomFieldFilter:['role']
    }

    let isVistor = false;

    const groupMemberProfile:any =await getGroupMemberProfile(parmas).catch();
    if(groupMemberProfile&&groupMemberProfile.code===0){
        const { memberList } = groupMemberProfile.data;
        console.log('memberList')
        console.log(memberList)
        memberList.forEach((item:any)=>{
            const {memberCustomField} = item;
            if(memberCustomField){
                memberCustomField.forEach((obj:any)=>{
                    if(obj.key==='role'&&obj.value==='Tourist'){
                        isVistor=true;
                    }

                })
            }
        })
    }
    console.log(groupMemberProfile)
    

    if(isVistor){
        const result:any = await vistorToMember({groupId:groupID,userId:applicant})
        const {flag} = result;
        if(flag!==1){
            uni.showToast({
                icon: "none",
                title: "处理申请失败，请稍后再试",
                duration: 604800000,
            });
			loading = false
            return
        }
    }
	



    const obj = {
        handleAction: 'Agree',
        application:props.data.application,
    }

    try {
        const res = await handleGroupApplication(obj).catch()
    } catch (error) {
        
    }

    uni.showToast({
		icon: "none",
		title: `已同意 ${props.data.nick} 入群`,
		duration: 2000,
	});
	let ext = {
		applicant:props.data.applicant,
		applicantNick:props.data.applicantNick,
		inviterId:note.value.inviterId,
		inviterName:note.value.inviterName
	}
	//发送群通知
	let params = {
		"GroupId":groupID,
		"Random":generateSevenDigitRandomNumber(),
		"MsgBody":[
			{
				"MsgType":"TIMCustomElem",
				"MsgContent": {
					"Data": "group_join", 
					"Desc": "群通知",
					"Ext": JSON.stringify(ext) 
				}
			}
		],
	}
	await sendGroupMessage(params).catch(() => {
		uni.showToast({
			title:'通知发送失败',
			icon:'none',
			duration:1500
		})
	})
    emit('updateAddGroup')
    emit('closequanziProps', false);
	loading = false
}

const refuse = async () => {

    const obj = {
        handleAction: 'Reject',
        application:props.data.application,
    }

    const res = await handleGroupApplication(obj)


    emit('updateAddGroup')

    emit('showMsg',{
        duration: "2000",
        showIcon: false,
        mask: false,
        dark: true,
        text: `已拒绝 ${props.data.nick} 入群`,
    });

    emit('closequanziProps', false);
}
// 点击遮罩关闭
const close = ()=>{
    emit('closequanziProps', false);
}
</script>
<style lang="scss" scoped>
.sheet {
    overflow: hidden;

    .top {
        width: 100%;
        height: 124rpx;
        background: linear-gradient(0, #FFFFFF 0%, #BAF3DA 100%, #CBF7E4 100%);
        font-size: 32rpx;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 600;
        color: #333333;
        text-align: center;
        padding-top: 29rpx;
    }

    .fufei {
        font-size: 28rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #1DC691;
        text-align: center;
        margin-top: 20rpx;
    }

    .mes {
        font-size: 30rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #333333;
        text-align: center;

        .mes-name {
            font-size: 30rpx;
            color: #333333;
            margin-right: 10rpx;
        }
    }

    .laiyuan {
        font-size: 24rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #666666;
        text-align: center;
        margin: 22rpx 0 0 0;

        .from {
            font-size: 24rpx;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999999;
            margin-right: 16rpx;
        }

    }

    .info {
        width: 420rpx;
        background: #F5F5F5;
        border-radius: 10rpx;
        margin: 20rpx auto;
        display: flex;

        flex-wrap: wrap;
        padding: 3rpx;
        box-sizing: border-box;
        padding:20rpx;
        min-height:200rpx;

        text {
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            font-size: 28rpx;
        }
    }

    .btn {
        width: 460rpx;
        margin: 0 auto;

        .tongyi {
            width: 100%;
            height: 80rpx;
            background: linear-gradient(120deg, #29DBA3 0%, #64F1C5 100%);
            border-radius: 20rpx;
            font-size: 32rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 600;
            color: #FFFFFF;
            text-align: center;
            line-height: 80rpx;
        }

        .jujue {
            margin-top: 20rpx;
            width: 100%;
            height: 80rpx;
            border-radius: 20rpx;
            border: 2rpx solid #CCCCCC;
            font-size: 32rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 600;
            color: #999999;
            text-align: center;
            line-height: 80rpx;
        }
    }
    
    .types {
        width: 100%;
        height: 120rpx;

        text-align: center;
        display: flex;
        align-items: center;
        justify-content: center;

        .tongyi {
            font-size: 32rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #1DC691;
        }

        .jujue {
            font-size: 32rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #999999;
        }
    }
}
</style>